/*
  this file contains functions related to obtaining and displaying
  the login page, most of these functions are assigned to much
  easier to use objects in the Layouts file
*/

function getLogin(id) {

  //drag.remove("huddle");

  if (huddle.isAuth()) { // if authenticated already, quickly switch to dashboard display
  
    page.switchContent("workspace",null);
  
  } else {
  
    dropdown.hide(); // hide the dropdown and any breadcrumbs that may persist
    
    breadcrumbs.hide();
  
    // write in the login screen
    $("#huddle>div>div").append("<div id='login'>Welcome to Huddle. Please sign in below:<form>Username<input type='text' id='userin' disabled='disabled' value=''>Password<input type='password' id='passin' disabled='disabled' value=''><button type='button' id='submit' disabled='disabled'>...</button></form><p><a href='http://www.huddle.net/what-is-huddle'>What is Huddle?</a> | <a href='http://www.huddle.net/huddle-price-plans/'>Register</a></p></div>");
    
    // when its faded in, activate the form
    setTimeout(function() {
    document.getElementById('userin').disabled = false;
    document.getElementById('passin').disabled = false;
    document.getElementById('submit').disabled = false;
    document.getElementById('submit').innerHTML = "Submit";
    document.getElementById('userin').focus();
    }, (page.getLoadspeed()/2)*1.1);
    
    // catch clicks to links and redirect to default browser
    $("a").click(function(e) {e.preventDefault(); air.navigateToURL(new air.URLRequest($(this).attr('href'))); });

    document.getElementById('submit').addEventListener("click", function() { login.display(document.getElementById("userin").value, document.getElementById("passin").value); });
    
    $("#userin, #passin, #submit").keypress(function(e) {
      login.keypress(document.getElementById("userin").value, document.getElementById("passin").value, e);
    } );
    
    page.loadComplete();

  }

}

// processes the login data
function loginProcess(user,pass) {

  if (user=="") {
  
    msgs.setError(2,"No username", "Please enter a username", 2000);
    
  }
  
  if (pass=="") {
  
    msgs.setError(2,"No password", "Please enter a valid password", 2000);
    
  }

  huddle.setAuthentication(user,pass); // set authentication data in huddle object

  page.displayPage("workspace",null); // go to the dashboard, will bounce back to login if request fails authentication
  
  download.authenticates(); // forces a handshake for file uploads / downloads so the dont fail

}